package com.uneed.api;

import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

import org.apache.http.NameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.baidu.mapapi.GeoPoint;
import com.uneed.api.util.Caller;
import com.uneed.api.util.pb;

public class HowcanGet2ApiImpl implements HowcanGet2Api {
	
	private static String GET_API = "http://www.haoshe.org/m/phpwind/mclientapi.php?";
	private static String M_ROOT = "http://www.haoshe.org/m/md28/";
	private static String M_API = M_ROOT + "mobile.php?";
	private static String M_CACHE = M_ROOT + "data/cachefiles/";

	public JSONObject checkAccount() throws  JSONException, WSError, IOException {
		return Caller.doPost(M_API + "act=user&op=checkAccount",null);		
	}

	public JSONObject getShopLists(GeoPoint gp) throws JSONException, WSError {
		return Caller.doPost(M_API + "act=list&catid=1&lat="+gp.getLatitudeE6()+"&lng="+gp.getLongitudeE6() ,null);
//		if (jsonString == null)
	}		

	public JSONObject getCategorys() throws JSONException, WSError {
		return Caller.doPost(M_CACHE + "category.php",null);
//		return doGet("http://www.haoshe.org/musicbox/modoer/howcan.php?act=list&catid=1");

	}		

	public JSONObject getRegions() throws JSONException, WSError {
		return Caller.doPost(M_CACHE + "area.php",null);
	}		

	
//	%5B%22cxandy%22%2C%22a14f6ecee905d1f9a20153059638105a%22%5D&sign=6e773095cd7fd54e676c3f6c5d7ee5c6&

	public JSONObject userLogin(String username, String password) throws JSONException, WSError {
		try {
		
//		    return new jb((JSONObject)kw.a(ky.a, "user.checkauth", localLinkedHashMap));
//		      localObject2 = (JSONArray)localLinkedHashMap;
		    JSONArray parms = new JSONArray();
		    parms.put(username);
		    parms.put(pb.c(password));
		    
		    String request_str = GET_API + "callback=user.checkauth&params=" + URLEncoder.encode(parms.toString());
	    
			return Caller.doPost(request_str,null);
		}	catch (NullPointerException e) {
			e.printStackTrace();
			throw new JSONException(e.getLocalizedMessage());
		}
	}

@Override
public JSONObject shopAdd(List<NameValuePair> params) throws JSONException,
		WSError, IOException {
	// TODO Auto-generated method stub
	String url= M_API + "act=member&ac=subject_add";
	return Caller.doPost(url,params);
}

@Override
public JSONObject userLogin(List<NameValuePair> params) throws  JSONException, WSError, IOException {
	String url= M_API + "module=login&op=login";
	return Caller.doPost(url,params);	
}

@Override
public JSONObject getShopInfo(int shopid) {
	// TODO Auto-generated method stub
	return Caller.doPost(M_API + "act=item_detail&id=" + shopid);
}

@Override
public JSONObject getReviews(int shopid) throws JSONException,
		WSError {
	// TODO Auto-generated method stub
	return Caller.doPost(M_API + "act=item_reviews&id=" + shopid);
}

@Override
public JSONObject getReviewLists(int shopid) throws JSONException, WSError {
	// TODO Auto-generated method stub
	return Caller.doPost(M_API + "act=item_reviews&id=" + shopid);
}

@Override
public JSONObject userPassportAutoLogin(String passport_type,String passport_id) throws JSONException, WSError {
	// TODO Auto-generated method stub
	return Caller.doPost(M_API + "module=login&op=passport_login&type=auto_login&passport_type=" + passport_type + "&passport_id=" + passport_id);
}

/*	@Override
	public String shopAdd(List<NameValuePair> params) throws JSONException,
			WSError, IOException {
		// TODO Auto-generated method stub
		String url="http://www.haoshe.org/m/modoer/howcan.php?act=member&ac=subject_add";
		return doPost(url,params);
	}*/


}
